package app

import (
	"net/http"
	"strings"

	"myserver/pkg/constant"

	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
)

// BindAndValid binds and validates data
func BindAndValid(c *gin.Context, form interface{}) (int, int, error) {
	err := c.Bind(form)
	if err != nil {
		return http.StatusBadRequest, constant.INVALID_PARAMS, err
	}

	valid := validation.Validation{}
	check, err := valid.Valid(form)
	if err != nil {
		return http.StatusInternalServerError, constant.ERROR, err
	}
	if !check {

		return http.StatusBadRequest, constant.INVALID_PARAMS, nil
	}

	return http.StatusOK, constant.SUCCESS, nil
}
func MarkErrors(errors []*validation.Error) string {
	var str []string
	for _, err := range errors {
		str = append(str, err.Key)
		str = append(str, err.Message)
	}

	return strings.Join(str, "")
}
